//
//  HJRepairTurnCarHeaderView.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/5/15.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJRepairTurnCarHeaderView.h"
#import "popContentViewController.h"
#import "DDPopoverBackgroundView.h"


#import "AppDelegate.h"

@interface HJRepairTurnCarHeaderView()<ContentViewControllerDelegate>
/**
 搜索数组
 */
@property(nonatomic,strong)NSArray*searchAry;
/**
 选择框
 */
@property(nonatomic,strong)NSArray*selectAry;
/**
 弹窗
 */
@property(nonatomic,strong)UIPopoverController*popCon;
@end
@implementation HJRepairTurnCarHeaderView

-(NSArray *)selectAry
{
    if (!_selectAry) {
        _selectAry=@[@"服务顾问",@"是否结清"];
    }
    return _selectAry;
}
-(NSArray *)searchAry
{
    if (!_searchAry) {
        _searchAry=@[@"工单号",@"车牌号"];
    }
    return _searchAry;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    WEAKSELF;
    CGFloat w=250;
    CGFloat H=35;
    for (int i=0; i<self.searchAry.count; i++) {
        UIView*subView=[[UIView alloc]init];
        [self addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i?weakSelf.mas_centerX:weakSelf);
            make.top.mas_equalTo(weakSelf).offset(20);
            make.size.mas_equalTo(CGSizeMake(w, H));
        }];
        UILabel*leftLabel=[[UILabel alloc]init];
        leftLabel.text=self.searchAry[i];
        leftLabel.font=[UIFont systemFontOfSize:15];
        [subView addSubview:leftLabel];
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(subView);
            make.width.mas_equalTo(70);
        }];
        UIView*bgView=[UIView new];
        bgView.backgroundColor=[UIColor grayColor];
        [subView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(subView);
            make.left.mas_equalTo(leftLabel.mas_right).offset(5);
        }];
        UITextField*textField=[[UITextField alloc]init];
        [subView addSubview:textField];
        textField.backgroundColor=[UIColor whiteColor];
        textField.tag=i;
        textField.textAlignment=NSTextAlignmentCenter;
        [textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.left.mas_equalTo(bgView).inset(1);
        }];
    }
    for (int i=0; i<self.selectAry.count; i++) {
        UIView*subView=[[UIView alloc]init];
        [self addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i?weakSelf.mas_centerX:weakSelf);
            make.top.mas_equalTo(weakSelf).offset(20*2+H);
            make.size.mas_equalTo(CGSizeMake(w, H));
        }];
        UILabel*leftLabel=[[UILabel alloc]init];
        leftLabel.text=self.selectAry[i];
        leftLabel.font=[UIFont systemFontOfSize:15];
        [subView addSubview:leftLabel];
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(subView);
            make.width.mas_equalTo(70);
        }];
      
        UIView*bgView=[UIView new];
        bgView.backgroundColor=[UIColor grayColor];
        [subView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(subView);
            make.left.mas_equalTo(leftLabel.mas_right).offset(5);
        }];
        
        UIButton*btn=[[UIButton alloc]init];
        [subView addSubview:btn];
        [btn setImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.left.mas_equalTo(bgView).inset(1);
        }];
        
        btn.tag=i;
        UILabel*contentLabel=[[UILabel alloc]init];
        [btn addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.mas_equalTo(btn);
        }];
        contentLabel.textAlignment=NSTextAlignmentCenter;
        contentLabel.text=@"< 空 >";
        contentLabel.textColor=[UIColor grayColor];
        contentLabel.font=HJFont(15);
        contentLabel.tag=10;
        if (i==0) {
            //默认顾问是登录账号
             NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
            contentLabel.text=[user objectForKey:@"USER_NAME"];
            [btn setTitle:[user objectForKey:@"EMPLOYEE_NO"] forState:UIControlStateNormal];
        }
    }
    
}
-(void)clickSelect:(UIButton*)btn{
    [self endEditing:YES];
    popContentViewController*contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
    contentCon.delegate = self;
    contentCon.btnSender=btn;
    UIPopoverController*popCon = [[UIPopoverController alloc] initWithContentViewController:contentCon];
    self.popCon=popCon;
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:242 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    [popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [popCon setPopoverContentSize:CGSizeMake(btn.bounds.size.width, 160)];
    [popCon presentPopoverFromRect:CGRectMake(0, 10, 105, 20) inView:btn permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    //DICT_YES_NO
    NSString*key=btn.tag?@"DICT_YES_NO":@"SERVICE_ADVISOR";
    NSArray *zidianArr=[self readershuDICimform][key];
    NSMutableArray *nameArr = [[NSMutableArray alloc] init];
    NSMutableArray *codeArr = [[NSMutableArray alloc] init];
    for(NSDictionary *dic in zidianArr)
    {
        [codeArr addObject:dic[@"CODE"]];
        [nameArr addObject:dic[@"NAME"]];
    }
    
    contentCon.list = nameArr;
    contentCon.codeAry=codeArr;
    [contentCon.myTableView reloadData];
}
-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}
-(void)textFieldChange:(UITextField*)textField{
    if (textField.tag) {
        //车牌号
        if ([_delegate respondsToSelector:@selector(repairTurnCarHeaderView:changeCarNumber:)]) {
            [_delegate repairTurnCarHeaderView:self changeCarNumber:textField.text];
        }
    }else{
        //工单号
        if ([_delegate respondsToSelector:@selector(repairTurnCarHeaderView:changeOrderNumber:)]) {
            [_delegate repairTurnCarHeaderView:self changeOrderNumber:textField.text];
        }
    }
}
#pragma mark --ContentViewControllerDelegate
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
    
}
-(void)popContentViewController:(popContentViewController *)contentViewController name:(NSString *)name code:(NSString *)code
{
    UIButton*btn=contentViewController.btnSender;
    [btn setTitle:code forState:UIControlStateNormal];
    UILabel*label=[btn viewWithTag:10];
    label.text=name;
    [self.popCon dismissPopoverAnimated:YES];
    if (btn.tag) {
        //是否结清
        if ([_delegate respondsToSelector:@selector(repairTurnCarHeaderView:changeIsSettlement:)]) {
            [_delegate repairTurnCarHeaderView:self changeIsSettlement:code];
        }
    }else{
        //服务顾问
        if ([_delegate respondsToSelector:@selector(repairTurnCarHeaderView:changeServiceAdviser:)]) {
            [_delegate repairTurnCarHeaderView:self changeServiceAdviser:code];
        }
    }
    
}
@end
